/**
 * @a https://leetcode.cn/problems/single-number-ii/description/
 */

#include "common.h"

class Solution
{
public:
    int singleNumber(vector<int> &nums)
    {
        int ret = 0;
        for (int i = 0; i < 32; ++i)
        {
            int sum = 0;
            for (auto num : nums)
            {
                if (num & (1 << i))
                    ++sum;
            }
            sum %= 3;
            if (sum == 1)
                ret |= (1 << i);
        }
        return ret;
    }
};

// 统一解法
#define N 2
class Solution
{
public:
    int singleNumber(vector<int> &nums)
    {
        int ret = 0;
        for (int i = 0; i < 32; ++i)
        {
            int sum = 0;
            for (auto num : nums)
            {
                if (num & (1 << i))
                    ++sum;
            }
            sum %= N;
            if (sum == 1)
                ret |= (1 << i);
        }
        return ret;
    }
};